/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "event_groups.h"
#include "lcd.h"
#include "keyled.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define BITMASK_LEY0 0x01
#define BITMASK_LEY1 0x04
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for Task_ScanKeys */
osThreadId_t Task_ScanKeysHandle;
const osThreadAttr_t Task_ScanKeys_attributes = {
  .name = "Task_ScanKeys",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityAboveNormal,
};
/* Definitions for Task_Buzzer */
osThreadId_t Task_BuzzerHandle;
const osThreadAttr_t Task_Buzzer_attributes = {
  .name = "Task_Buzzer",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Task_LED */
osThreadId_t Task_LEDHandle;
const osThreadAttr_t Task_LED_attributes = {
  .name = "Task_LED",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for EventGroup */
osEventFlagsId_t EventGroupHandle;
const osEventFlagsAttr_t EventGroup_attributes = {
  .name = "EventGroup"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_ScanKeys(void *argument);
void AppTask_Buzzer(void *argument);
void AppTask_LED(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Task_ScanKeys */
  Task_ScanKeysHandle = osThreadNew(AppTask_ScanKeys, NULL, &Task_ScanKeys_attributes);

  /* creation of Task_Buzzer */
  Task_BuzzerHandle = osThreadNew(AppTask_Buzzer, NULL, &Task_Buzzer_attributes);

  /* creation of Task_LED */
  Task_LEDHandle = osThreadNew(AppTask_LED, NULL, &Task_LED_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* Create the event(s) */
  /* creation of EventGroup */
  EventGroupHandle = osEventFlagsNew(&EventGroup_attributes);

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_ScanKeys */
/**
  * @brief  Function implementing the Task_ScanKeys thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_AppTask_ScanKeys */
void AppTask_ScanKeys(void *argument)
{
  /* USER CODE BEGIN AppTask_ScanKeys */
	LCD_ShowStr(10, 150, "Current event bits= ");
	uint16_t LcdX=LCD_CurX;
	KEYS  keycode=KEY_NONE;
	for(;;)
	{
		EventBits_t curBits=xEventGroupGetBits(EventGroupHandle);
		LCD_ShowUintHex(LcdX, 150, curBits, 1);
		keycode=ScanPressedKey(50);
		switch(keycode)
		{
		case KEY0:
			xEventGroupSetBits(EventGroupHandle, BITMASK_LEY0);
			break;
		case KEY1:
			xEventGroupSetBits(EventGroupHandle, BITMASK_LEY1);
			break;
		case KEY2:
			xEventGroupClearBits(EventGroupHandle, BITMASK_LEY0|BITMASK_LEY1);
			break;
		}
		if(keycode==KEY_NONE)
			vTaskDelay(50);
		else
			vTaskDelay(200);
	}
  /* USER CODE END AppTask_ScanKeys */
}

/* USER CODE BEGIN Header_AppTask_Buzzer */
/**
* @brief Function implementing the Task_Buzzer thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_AppTask_Buzzer */
void AppTask_Buzzer(void *argument)
{
  /* USER CODE BEGIN AppTask_Buzzer */
	BaseType_t clearOnExit=pdTRUE;
	BaseType_t waitForAllBits=pdTRUE;
	EventBits_t bitToWait=BITMASK_LEY0|BITMASK_LEY1;
  /* Infinite loop */
  for(;;)
  {
    EventBits_t result= xEventGroupWaitBits(EventGroupHandle, bitToWait, clearOnExit, waitForAllBits, portMAX_DELAY);
	for(uint8_t i=0;i<10;i++)
	{
		Buzzer_Toggle();
		vTaskDelay(pdMS_TO_TICKS(500));
	}
  }
  /* USER CODE END AppTask_Buzzer */
}

/* USER CODE BEGIN Header_AppTask_LED */
/**
* @brief Function implementing the Task_LED thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_AppTask_LED */
void AppTask_LED(void *argument)
{
  /* USER CODE BEGIN AppTask_LED */
	BaseType_t clearOnExit=pdTRUE;
	BaseType_t waitForAllBits=pdTRUE;
	EventBits_t bitToWait=BITMASK_LEY0|BITMASK_LEY1;
  /* Infinite loop */
  for(;;)
  {
	EventBits_t result= xEventGroupWaitBits(EventGroupHandle, bitToWait, clearOnExit, waitForAllBits, portMAX_DELAY);
	for(uint8_t i=0;i<10;i++)
	{
		LED0_Toggle();
		vTaskDelay(pdMS_TO_TICKS(500));
	}
  }
  /* USER CODE END AppTask_LED */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

